﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LiteDB;
using MyFound.Wpf.Windows;

namespace MyFound.Wpf
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            mainGrid.ItemsSource = GetData();

            System.Windows.Forms.NotifyIcon notifyIcon=new NotifyIcon();
            notifyIcon.Icon = System.Drawing.Icon.FromHandle(MyFound.Wpf.Properties.Resources.MalClean.GetHicon());
            notifyIcon.Visible = true;
            notifyIcon.Click += notifyIcon_Click;
            System.Windows.Forms.MenuItem menuItem = new System.Windows.Forms.MenuItem();
            menuItem.Text = "关闭";
            menuItem.Click += menuItem_Click;
            notifyIcon.ContextMenu =new System.Windows.Forms.ContextMenu();
            notifyIcon.ContextMenu.MenuItems.Add(menuItem);

        }

        void menuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = WindowState.Minimized;
            this.Close();
        }

        void notifyIcon_Click(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.WindowState = WindowState.Normal;
            }
        }

        private IEnumerable<DataGridRowModel> GetData()
        {
            var rowModels = new List<DataGridRowModel>();
            var liteDatabase = new LiteDatabase("lite.db");
            var foundCol = liteDatabase.GetCollection<Found>("Found");
            var founds = foundCol.FindAll();
            var tradeCol = liteDatabase.GetCollection<Trade>("Trade");
            var trades = tradeCol.Find(x => x.RealCount > 0);

            founds.ToList().ForEach(found =>
            {
                found.GrEquity = FoundApi.GetGrEquity(found.Code);
                foundCol.Update(found);
            });

            //Console.Clear();
            trades.OrderBy(x => x.FdCode).ToList().ForEach(trade =>
            {
                DataGridRowModel model = new DataGridRowModel();
                rowModels.Add(model);
                var found = founds.First(f => f.Code == trade.FdCode);

                //Console.CursorVisible = false;

                var syl = (found.Equity - trade.RealEquity)/found.Equity;
                var gssyl = found.GrEquity > 0 ? (found.GrEquity - trade.RealEquity)/found.GrEquity : 0;

                model.GProfitRate = gssyl;
                model.ProfitRate = syl;
                model.Code = trade.FdCode;
                model.Count = trade.RealCount;
                model.Equity = trade.RealEquity;
                model.GEquity = found.GrEquity;
                model.Profit = (syl*trade.RealCount*trade.RealEquity);
                model.GProfit = (gssyl*trade.RealCount*trade.RealEquity);
                model.Id = trade.Id;

            });
           

            return rowModels;
        }

        private void WinLoad(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem((state) =>
            {
                while (true)
                {
                    MyFound.Program.UpdateFoundEquity();
                    var data = GetData();
                    Dispatcher.Invoke(() =>
                    {
                        var index = mainGrid.SelectedIndex;
                        mainGrid.ItemsSource = data;
                        mainGrid.SelectedIndex = index;
                    });
                    Thread.Sleep(5000);
                }
            });
        }

        private void MainGrid_OnLoadingRow(object sender, DataGridRowEventArgs e)
        {
            
        }

        private void MenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            AddFoundWindow addFoundWindow=new AddFoundWindow();
            addFoundWindow.ShowDialog();
        }

        private void ShuHuiMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
           SaleWindow saleWindow=new SaleWindow();
            saleWindow.Show();
        }

        private void MainWindow_OnClosing(object sender, CancelEventArgs e)
        {
            if (this.WindowState != WindowState.Minimized)
            {
                e.Cancel = true;
                this.WindowState = WindowState.Minimized;
            }
        }

        private void MainWindow_OnStateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized)
            {
                this.ShowInTaskbar = true;
            }else if (this.WindowState == WindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            }
        }
    }
}
